# -*- coding: utf-8 -*-
import random
import threading
import time
from threading import Semaphore

MAX_CONNECTIONS = 2
database_semaphore = Semaphore(MAX_CONNECTIONS)


def use_database():
    with database_semaphore:
        print(f"Using database connection. Thread: {threading.current_thread().name}")
        time.sleep(random.uniform(1, 3))


threads = []

for _ in range(10):
    thread = threading.Thread(target=use_database)
    thread.start()
    threads.append(thread)

for thread in threads:
    thread.join()
